/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PHS; 
import java.sql.*;
import java.util.*;
import java.io.*;
/**
 *
 * @author Rajendar
 */
public class TestReport implements Serializable
{
    String url="jdbc:db2://localhost:50000/PHSYSTEM";
    String driver="com.ibm.db2.jcc.DB2Driver";
    String user="db2admin";
    String pwd="db2admin";
    Statement stmt;
    Connection conn=null;
    ResultSet rs;
    int patientId;
    int testId;
//    String testName;
    String factor;
    String factorValue;
    String comments;
    ArrayList testIdList=new ArrayList();
    ArrayList testNameList=new ArrayList();
    ArrayList dateList=new ArrayList();
    public TestReport()
    {
        try{
        Class.forName(driver);
        conn=DriverManager.getConnection(url,user,pwd);
//        stmt=conn.createStatement();
        System.out.println("connection :"+conn);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void setPatientId(int patientId)
    {
        this.patientId=patientId;
        System.out.println("setter method for patientId:"+this.patientId);
    }
    public void setFactor(String factor)
    {
        this.factor=factor;
        System.out.println("setter method for factor:"+this.factor);
    }
    public void setFactorValue(String factorValue)
    {
        this.factorValue=factorValue;
        System.out.println("setter method for factorValue:"+this.factorValue);
    }
    public void setComments(String comments)
    {
        this.comments=comments;
        System.out.println("setter method for comments:"+this.comments);
    }
    
    public void setTestId(int testId)
    {
        this.testId=testId;
        System.out.println("setter method for testId:"+this.testId);
    }
    /*public void setTestName(ArrayList testName)
    {
        this.testName=testName;
        System.out.println("setter metho for testName:"+this.testName);
    }
   
    public void setDate(ArrayList date)
    {
        this.date=date;
        System.out.println("setter method for date:"+this.date);
    }*/
    public void getTestReport(int patientId)
    {
        String query="SELECT tp.testId,ti.testName,tp.date"+
                     " FROM TestPrescribed tp,TestInfo ti"+
                     " WHERE tp.testId=ti.testId AND tp.status='WAIT_TEST' AND tp.patientId="+patientId;
        System.out.println(query);
        try
        {
            stmt=conn.createStatement();
            rs=stmt.executeQuery(query);
            System.out.println(rs);
            while(rs.next())
            {
            testIdList.add(rs.getInt("testId"));
            testNameList.add(rs.getString("testName"));
            dateList.add(rs.getString("date"));
            }
        }
        catch(SQLException e)
        {
            System.out.println(e);
        }
     }
    public int getPatientId()
    {
        System.out.println("getPatientId:"+patientId);
        return patientId;
    }
//    public int getTestId1()
//    {
//        System.out.println("getTestId1:"+testId1);
//        return testId1;
//    }
//    public String getTestName1()
//    {
//        System.out.println("getTestName1:"+testName1);
//        return testName1;
//    }

    public ArrayList getTestIdList()
    {
        System.out.println("gettestId:"+testIdList);
        return testIdList;
    }
    public ArrayList getTestNameList()
    {
        System.out.println("getTestName:"+testNameList);
        return testNameList;
    }
    public ArrayList getDateList()
    {
        System.out.println("getDate:"+dateList);
        return dateList;
    }

    public void writeTestReport()
    {
         int rowcount=0;
          PreparedStatement pstmt;
          String insert="INSERT INTO PatientTestReport(patientId,testId,factor,factorValue,date,comments)" +
                " VALUES(?,?,?,?,CURRENT TIMESTAMP,?)";
      try
        {
         
          /*Connection con=null;
          Class.forName(driver);
          con=DriverManager.getConnection(url,user,pwd);*/
           
        conn.setAutoCommit(false);
        pstmt=conn.prepareStatement(insert);
        pstmt.setInt(1,patientId);
        pstmt.setInt(2,testId);
        pstmt.setString(3,factor);
        pstmt.setString(4,factorValue);
//        pstmt.setDate(5,date);
        pstmt.setString(5,comments);
        rowcount=pstmt.executeUpdate();
        conn.commit();
        
      }

        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }
//    public void updateStatus()
//    {
//        String query="";
//        try
//        {
//            stmt=conn.createStatement();
//            rs=stmt.executeQuery(query);
//            System.out.println(rs);
//            while(rs.next())
//            {
//            testIdList.add(rs.getInt("testId"));
//            testNameList.add(rs.getString("testName"));
//            dateList.add(rs.getString("date"));
//            }
//        }
//        catch(SQLException e)
//        {
//            System.out.println(e);
//        }
//     }
        
    }
